package fr.noe.amicaleloisirs.concours.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.noe.amicaleloisirs.concours.business.Player;
import fr.noe.amicaleloisirs.concours.interfaces.CallbackCreationPlayer;
import fr.noe.amicaleloisirs.concours.interfaces.CallbackUpdatePlayer;
import fr.noe.amicaleloisirs.concours.interfaces.CallbackValidCancel;

/**
 * 
 * @author Sébastien PREVOST
 *
 */
public class CreatePlayerView extends PopupPanel {

	public final TextBox nomField = new TextBox();
	public final TextBox prenomField = new TextBox();
	Button validerButton = new Button("Valider");
	Button annulerButton = new Button("Annuler");
	Player playerToUpdate;

	private static final String TEXTBOX_ERROR_CSS = "error-text-box";
	private boolean nomFieldEmpty = false;
	private boolean prenomFieldEmpty = false;

	/**
	 * Constructeur
	 * 
	 * @param label
	 *            : texte à afficher
	 */
	public CreatePlayerView(String label, Player p,
			final CallbackValidCancel callback) {
		setGlassEnabled(true);
		setModal(true);
		setSize("300px", "165px");

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(10);
		setWidget(verticalPanel);
		verticalPanel.setSize("288px", "153px");

		Label title = new Label(label);
		title.getElement().getStyle().setProperty("fontWeight", "bold");
		verticalPanel.add(title);
		title.setSize("100%", "10%");

		VerticalPanel verticalPanel_1 = new VerticalPanel();
		verticalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		verticalPanel_1
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(verticalPanel_1);
		verticalPanel.setCellHorizontalAlignment(verticalPanel_1,
				HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(verticalPanel_1,
				HasVerticalAlignment.ALIGN_BOTTOM);
		verticalPanel_1.setSize("100%", "80%");

		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_1
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel_1.add(horizontalPanel_1);
		horizontalPanel_1.setHeight("50%");

		Label lblNewLabel = new Label("Nom: ");
		lblNewLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_1.add(lblNewLabel);
		lblNewLabel.setSize("50px", "18px");
		horizontalPanel_1.setCellHorizontalAlignment(lblNewLabel,
				HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_1.setCellHeight(lblNewLabel, "50%");
		horizontalPanel_1.setCellWidth(lblNewLabel, "20%");

		this.playerToUpdate = p;

		KeyUpHandler kuh = new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				_onKeyUp(event, callback);

			}
		};
		nomField.addKeyPressHandler(new KeyPressHandler() {

			public void onKeyPress(KeyPressEvent event) {
				if (nomFieldEmpty) {
					if (isLetter(event.getCharCode())) {
						nomFieldEmpty = false;
						nomField.removeStyleName(TEXTBOX_ERROR_CSS);
					}
				}
			}
		});
		prenomField.addKeyPressHandler(new KeyPressHandler() {

			public void onKeyPress(KeyPressEvent event) {
				if (prenomFieldEmpty) {
					if (isLetter(event.getCharCode())) {
						prenomFieldEmpty = false;
						prenomField.removeStyleName(TEXTBOX_ERROR_CSS);
					}
				}
			}
		});
		nomField.addKeyUpHandler(kuh);

		if (p != null) {
			nomField.setText(p.getLastname());
		}
		prenomField.addKeyUpHandler(kuh);
		if (p != null) {
			prenomField.setText(p.getFirstname());
		}
		horizontalPanel_1.add(nomField);

		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		horizontalPanel_2
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel_1.add(horizontalPanel_2);
		horizontalPanel_2.setHeight("50%");

		Label lblNewLabel_1 = new Label("Prénom: ");
		lblNewLabel_1
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_2.add(lblNewLabel_1);
		lblNewLabel_1.setSize("50px", "18px");
		horizontalPanel_2.setCellHeight(lblNewLabel_1, "50%");
		horizontalPanel_2.setCellWidth(lblNewLabel_1, "20%");
		horizontalPanel_2.setCellHorizontalAlignment(lblNewLabel_1,
				HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel_2.add(prenomField);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		horizontalPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(horizontalPanel);
		verticalPanel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setSize("100%", "10%");

		horizontalPanel.add(validerButton);
		horizontalPanel.setCellHorizontalAlignment(validerButton,
				HasHorizontalAlignment.ALIGN_CENTER);
		validerButton.setSize("77px", "28px");

		annulerButton.setFocus(false);
		horizontalPanel.add(annulerButton);
		annulerButton.setSize("77px", "28px");

		this.center();

		nomField.setCursorPos(nomField.getText().length());
		nomField.setFocus(true);

		validerButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				valider(callback);
			}
		});

		annulerButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				annuler(callback);
			}
		});
	}

	private void valider(CallbackValidCancel callback) {
		if (!nomField.getText().isEmpty() && !prenomField.getText().isEmpty()) {
			if (callback instanceof CallbackCreationPlayer)
				((CallbackCreationPlayer) callback).onValidate(
						nomField.getText(), prenomField.getText());
			else {

				// si modification apportée
				if (!playerToUpdate.getFirstname()
						.equals(prenomField.getText())
						|| !playerToUpdate.getLastname().equals(
								nomField.getText())) {
					playerToUpdate.setFirstname(prenomField.getText());
					playerToUpdate.setLastname(nomField.getText());
					((CallbackUpdatePlayer) callback)
							.onValidate(playerToUpdate);
				}
			}
			CreatePlayerView.this.hide();
		} else {
			if (nomField.getText().isEmpty()) {
				nomField.addStyleName(TEXTBOX_ERROR_CSS);
				nomFieldEmpty = true;
			}
			if (prenomField.getText().isEmpty()) {
				prenomField.addStyleName(TEXTBOX_ERROR_CSS);
				prenomFieldEmpty = true;
			}
		}
	}

	private void annuler(CallbackValidCancel callback) {
		if (callback instanceof CallbackCreationPlayer)
			((CallbackCreationPlayer) callback).onCancel(nomField.getText(),
					prenomField.getText());
		else {
			((CallbackUpdatePlayer) callback).onCancel(playerToUpdate);
		}
		CreatePlayerView.this.hide();
	}

	public void _onKeyUp(KeyUpEvent event, CallbackValidCancel callback) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			valider(callback);
		} else if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
			annuler(callback);
		}

	}
	
	private boolean isLetter(char c) {
		return ((c >= 'a' && c<= 'z') || (c >= 'A' && c<= 'Z'));
	}

}
